/** Class Name: OORTelesalesUtility
 * Author: Accenture
 * Date: 3-March-2011
 * Requirement/Project Name: OOR Telesales
 * Requirement Description:  Utitlity class that helps to reduce Redundant code 
 **/
public class OORTelesalesUtility
{
    private static Map<string, ID> salesforceIDMap = new Map<string, ID>();
    
    // Cannot instantiate  utility class 
    private OORTelesalesUtility(){}
    
    /**
     *   returns Salesforce record ID
     *
     */
    public static Set<ID> getRecordID(List <Sobject> sobjList)
    {
        final Set<id> recId = new Set<ID>();
        if(sobjList != null)
        {
            for(Sobject sObj:sobjList)
            {
                recId.add(sObj.id);
            }
         }
         return recId;
    }
    
    /**
     *   Covert list to map with ID and Sobject map
     *   @returns Map 
     *
     */
    public static Map<ID,Sobject> covertListToMap (List <Sobject> sobjList)
    {
        final Map<ID,Sobject> listMap = new Map<ID,Sobject>();
        
        if(sobjList != null)
        {
            for(Sobject sObj:sobjList)
            {
                listMap.put(sObj.id , sObj);
            }
         }
         return listMap;
    }
    
    /**
     *  Returns a random integer between 0 and maxInt. The number will never equal maxInt though.
     *  Close enough to random for our purposes!
     */
    public static  Integer getRandomInt(Integer maxInt)
    {
        return Math.abs(Math.mod(Crypto.getRandomInteger(), maxInt)); 
    }
    
    /**
     *  Returns queue ID or null
     *  Note : DeveloperName  API is supported from 24.0+  Version .
     */
    public static ID getQueueID(String queueDeveloperName)
    {
        final String mapKey = 'QUEUE-'+queueDeveloperName;
        ID queueID  = null;
        List<Group> groupList = null;
        queueID = salesforceIDMap.get(mapKey);
        
        // request the database only if the data is not fetched in the past.
        if(queueID == null)
        {
            groupList = [select id from Group where DeveloperName = :queueDeveloperName ];
            if(groupList.size() > 0)
            {
                queueID = groupList.get(0).id;
                salesforceIDMap.put(mapKey, queueID);
            }
        }
        return queueID  ;       
    }
    
    /**
     *   To display apex page error msg.
     *       
     */
    public static void displayErrorMsg(String errMsg)
    {
       
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,errMsg);         
        ApexPages.addMessage(msg);             
    }

    /**
     *  Filter Sobject based on record type
     *  General used with Shared object like Lead, Task in Triggers
     *
     */    
    
    public static List<SObject>  filterSobjectBasedOnRecType (List<Sobject> sobjList , String objectType ,String recType)
    {
         final ID sobjRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName(objectType, recType);
         final List<Sobject> tempObjList  = new List<Sobject>();
         
         for(Sobject sObj : sobjList )
         {
            if(sobjRecordTypeID  ==  sObj.get('recordtypeid') )
            {
                 tempObjList.add(sObj);               
            }
         }
         return tempObjList;
    }
    
  /**
   *  Test method 
   */
  static testMethod void testOORTelesalesUtility() 
  {
      ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Lead', OORTelesalesTextConstant.LOCAL_ADVERTISER_LEAD_REC_TYPE);   
      List<Sobject>  uList = new List<Sobject>(); 
      Profile pro = [select id from profile where name='System Administrator'];
      User usr = new User(alias = 'sysad', email='sysadmin@accenture.com',emailencodingkey='UTF-8', lastname='Tes12', languagelocalekey='en_US',localesidkey='en_US', profileid = pro.Id,timezonesidkey='Asia/Calcutta', username='testad123@testorg.com');
      Insert usr;    
      uList.add(usr);
      Lead  le  = new Lead(FirstName='testFirstName',LastName = 'ash', company='ABc',Lead_Status_ReadOnly__c = 'New' ,Status='New',RecordTypeId= localAdvertiserRecordTypeID );
      getRecordID(uList);
      getRandomInt(3);
      covertListToMap (uList );
      uList.clear();
      uList.add(le);
      getQueueID(OORTelesalesTextConstant.OOR_TELE_CAMPAIGN_LEAD_QUEUE);
      filterSobjectBasedOnRecType (uList ,'Lead', OORTelesalesTextConstant.LOCAL_ADVERTISER_LEAD_REC_TYPE);   
      displayErrorMsg('xyx');
  }
}